package cfca.seal.dao.main.impl;

import cfca.seal.bean.SealCert;
import cfca.seal.dao.SmartDao;
import cfca.seal.dao.main.ISealCertDao;
import cfca.seal.exception.CodeException;
import cfca.seal.util.StringUtil;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class SealCertDao
  implements ISealCertDao
{
  private static String tableName = "印章证书";
  private static final Logger log = LoggerFactory.getLogger("web_sys");

  @Resource
  private SmartDao smartDao;

  public boolean insert(SealCert sealCert) throws CodeException {
    try { String sql = "INSERT INTO SEAL_CERT (ID, ALIAS, ORGANIZATION_ID, DEVICE, CERT_ALG, STATUS, IMPORT_DATE, CREATE_TIME) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
      Object[] args = { sealCert.getId(), sealCert.getAlias(), sealCert.getOrganizationId(), sealCert.getDevice(), sealCert.getCertAlg(), sealCert.getStatus(), changeDateType(sealCert.getImportDate()), new Date() };

      log.debug("insert seal_cert sql: " + sql + args);
      int i = this.smartDao.update(sql, args);
      return i > 0;
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1002", tableName + "表插入失败", e);
    }
  }

  public List<SealCert> getAll(String org) throws CodeException
  {
    try {
      String sql = "SELECT * FROM SEAL_CERT";
      if (StringUtil.isNotEmpty(org)) {
        sql = sql + " WHERE ORGANIZATION_ID = '" + org + "'";
      }
      sql = sql + " ORDER BY CREATE_TIME DESC ";
      log.debug("select getAll sql by org: " + sql);
      return this.smartDao.getList(SealCert.class, sql);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }

  public void update(SealCert sealCert) throws CodeException
  {
    try {
      String sql = "UPDATE SEAL_CERT SET KEY_ID = ?, KEY_LENGTH = ?, P10 = ?, PRIVATE_KEY_ENCRYPTED_DATA = ?, PUBLICK_CERT_DATA = ?, PASSWORD = ?, STATUS = ? WHERE ID = ?";

      log.debug("update seal_cert sql: " + sql);
      Object[] args = { sealCert.getKeyId(), sealCert.getKeyLength(), sealCert.getP10(), sealCert.getPrivateKeyEncryptedData(), sealCert.getPublickCertData(), sealCert.getPassword(), sealCert.getStatus(), sealCert.getId() };

      this.smartDao.update(sql, args);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1003", tableName + "表更新失败", e);
    }
  }

  public boolean deleteById(String id) throws CodeException
  {
    try {
      String sql = "DELETE FROM SEAL_CERT WHERE ID = ?";
      Object[] args = { id };
      log.debug("delete seal_cert sql by id: " + sql + args);
      int i = this.smartDao.update(sql, args);
      return i > 0;
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1004", tableName + "表删除失败", e);
    }
  }

  public SealCert getSealCertById(String id) throws CodeException
  {
    try {
      String sql = "SELECT * FROM SEAL_CERT WHERE ID = ?";
      Object[] args = { id };
      log.debug("select seal_cert sql by id: " + sql + args);
      return (SealCert)this.smartDao.getBean(SealCert.class, sql, args);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }

  public SealCert getSealCertByKeyID(String keyID) throws CodeException {
    try {
      String sql = "SELECT * FROM SEAL_CERT WHERE KEY_ID = ?";
      Object[] args = { keyID };
      log.debug("select seal_cert sql by key_id: " + sql + args);
      return (SealCert)this.smartDao.getBean(SealCert.class, sql, args);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }

  private Timestamp changeDateType(Date date) {
    Timestamp sqlDate = null;
    if (date != null) {
      sqlDate = new Timestamp(date.getTime());
    }
    return sqlDate;
  }

  public List<SealCert> getAllForSelectShow(String orgid, String certType) throws CodeException
  {
    try {
      String sql = "SELECT * FROM SEAL_CERT WHERE STATUS=? AND DEVICE=? ";
      List args = new ArrayList();
      args.add("2");
      args.add(certType);
      if (StringUtil.isNotEmpty(orgid)) {
        sql = sql + " AND ORGANIZATION_ID = ?";
        args.add(orgid);
      }
      log.debug("select getAllForSelectShow sql: " + sql + args);
      return this.smartDao.getList(SealCert.class, sql, args.toArray());
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }

  public List<String> getSealCertKeyIDForSelectShow() throws CodeException
  {
    try {
      String sql = "SELECT KEY_ID FROM SEAL_CERT WHERE KEY_ID IS NOT NULL ORDER BY KEY_ID ASC";
      log.debug("getSealCertKeyIDForSelectShow: " + sql);
      return this.smartDao.getJdbcTemplate().queryForList(sql, String.class);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }
}